//! WebSocket数据模型
//! 
//! 定义WebSocket消息的数据结构

use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};

/// WebSocket流消息类型
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "stream", content = "data")]
pub enum WebSocketStreamMessage {
    /// 实时价格
    #[serde(rename = "btcusdt@ticker")]
    Ticker(TickerStream),
    /// 订单簿
    #[serde(rename = "btcusdt@depth")]
    Depth(DepthStream),
    /// K线数据
    #[serde(rename = "btcusdt@kline_1m")]
    Kline(KlineStream),
    /// 交易流
    #[serde(rename = "btcusdt@trade")]
    Trade(TradeStream),
    /// 订单更新
    #[serde(rename = "orderUpdate")]
    OrderUpdate(OrderUpdateStream),
    /// 账户更新
    #[serde(rename = "accountUpdate")]
    AccountUpdate(AccountUpdateStream),
    /// 错误消息
    #[serde(rename = "error")]
    Error(WebSocketError),
}

/// 实时价格流
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TickerStream {
    pub symbol: String,
    pub price: Decimal,
    pub bid_price: Decimal,
    pub ask_price: Decimal,
    pub volume: Decimal,
    pub quote_volume: Decimal,
    pub open_price: Decimal,
    pub high_price: Decimal,
    pub low_price: Decimal,
    pub close_price: Decimal,
    pub price_change: Decimal,
    pub price_change_percent: Decimal,
    pub weighted_avg_price: Decimal,
    pub prev_close_price: Decimal,
    pub last_qty: Decimal,
    pub bid_qty: Decimal,
    pub ask_qty: Decimal,
    pub open_time: u64,
    pub close_time: u64,
    pub first_id: u64,
    pub last_id: u64,
    pub count: u64,
}

/// 订单簿流
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DepthStream {
    pub symbol: String,
    pub bids: Vec<(Decimal, Decimal)>,
    pub asks: Vec<(Decimal, Decimal)>,
    pub last_update_id: u64,
}

/// K线数据流
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KlineStream {
    pub symbol: String,
    pub open_time: u64,
    pub close_time: u64,
    pub open: Decimal,
    pub high: Decimal,
    pub low: Decimal,
    pub close: Decimal,
    pub volume: Decimal,
    pub quote_volume: Decimal,
    pub trades: u64,
    pub taker_buy_base_volume: Decimal,
    pub taker_buy_quote_volume: Decimal,
    pub is_final: bool,
}

/// 交易流
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TradeStream {
    pub symbol: String,
    pub price: Decimal,
    pub quantity: Decimal,
    pub trade_time: u64,
    pub is_buyer_maker: bool,
    pub trade_id: u64,
}

/// 订单更新流
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OrderUpdateStream {
    pub symbol: String,
    pub order_id: u64,
    pub client_order_id: String,
    pub side: String,
    pub order_type: String,
    pub order_status: String,
    pub price: Decimal,
    pub quantity: Decimal,
    pub executed_qty: Decimal,
    pub cummulative_quote_qty: Decimal,
    pub time_in_force: String,
    pub order_time: u64,
    pub update_time: u64,
    pub is_working: bool,
}

/// 账户更新流
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccountUpdateStream {
    pub balances: Vec<BalanceUpdate>,
    pub positions: Vec<PositionUpdate>,
}

/// 余额更新
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BalanceUpdate {
    pub asset: String,
    pub wallet_balance: Decimal,
    pub cross_wallet_balance: Decimal,
    pub balance_change: Decimal,
}

/// 持仓更新
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PositionUpdate {
    pub symbol: String,
    pub position_amount: Decimal,
    pub entry_price: Decimal,
    pub accumulated_realized: Decimal,
    pub unrealized_pnl: Decimal,
    pub margin_type: String,
    pub isolated_wallet: Decimal,
    pub is_auto_add_margin: String,
    pub position_side: String,
    pub notional: Decimal,
    pub isolated_margin: Decimal,
    pub is_isolated: bool,
    pub update_time: u64,
}

/// WebSocket错误
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebSocketError {
    pub code: i32,
    pub msg: String,
}

/// 用户数据流消息
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserDataStreamMessage {
    #[serde(rename = "e")]
    pub event_type: String,
    #[serde(rename = "E")]
    pub event_time: u64,
    #[serde(flatten)]
    pub data: serde_json::Value,
}

/// 订单更新消息
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OrderUpdateMessage {
    #[serde(rename = "e")]
    pub event_type: String,
    #[serde(rename = "E")]
    pub event_time: u64,
    #[serde(rename = "s")]
    pub symbol: String,
    #[serde(rename = "c")]
    pub client_order_id: String,
    #[serde(rename = "S")]
    pub side: String,
    #[serde(rename = "o")]
    pub order_type: String,
    #[serde(rename = "f")]
    pub time_in_force: String,
    #[serde(rename = "q")]
    pub quantity: Decimal,
    #[serde(rename = "p")]
    pub price: Decimal,
    #[serde(rename = "P")]
    pub stop_price: Decimal,
    #[serde(rename = "F")]
    pub iceberg_quantity: Decimal,
    #[serde(rename = "g")]
    pub order_list_id: i64,
    #[serde(rename = "C")]
    pub orig_client_order_id: String,
    #[serde(rename = "x")]
    pub execution_type: String,
    #[serde(rename = "X")]
    pub order_status: String,
    #[serde(rename = "r")]
    pub order_reject_reason: String,
    #[serde(rename = "i")]
    pub order_id: u64,
    #[serde(rename = "l")]
    pub last_executed_quantity: Decimal,
    #[serde(rename = "z")]
    pub cumulative_filled_quantity: Decimal,
    #[serde(rename = "L")]
    pub last_executed_price: Decimal,
    #[serde(rename = "n")]
    pub commission_amount: Decimal,
    #[serde(rename = "N")]
    pub commission_asset: String,
    #[serde(rename = "T")]
    pub transaction_time: u64,
    #[serde(rename = "t")]
    pub trade_id: u64,
    #[serde(rename = "I")]
    pub ignore: u64,
    #[serde(rename = "w")]
    pub is_order_working: bool,
    #[serde(rename = "m")]
    pub is_trade_maker: bool,
    #[serde(rename = "M")]
    pub ignore_2: bool,
    #[serde(rename = "O")]
    pub order_creation_time: u64,
    #[serde(rename = "Z")]
    pub cumulative_quote_asset_transacted_quantity: Decimal,
    #[serde(rename = "Y")]
    pub last_quote_asset_transacted_quantity: Decimal,
    #[serde(rename = "Q")]
    pub quote_order_qty: Decimal,
}

/// 账户更新消息
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccountUpdateMessage {
    #[serde(rename = "e")]
    pub event_type: String,
    #[serde(rename = "E")]
    pub event_time: u64,
    #[serde(rename = "m")]
    pub event_reason_type: String,
    #[serde(rename = "B")]
    pub balances: Vec<BalanceUpdate>,
    #[serde(rename = "P")]
    pub positions: Vec<PositionUpdate>,
}
